package com.vinny.springbootflowable2.model;

import com.vinny.springbootflowable2.common.constant.ApiResultCode;
import lombok.Data;

/**
 * 统一API响应结果
 * @param <T> 响应数据类型
 */
@Data
public class ApiResponse<T> {
    /**
     * 状态码
     */
    private int code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    public static <T> ApiResponse<T> success(T data) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setCode(ApiResultCode.SUCCESS);
        response.setData(data);
        return response;
    }

    public static <T> ApiResponse<T> success(String message, T data) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setCode(ApiResultCode.SUCCESS);
        response.setMessage(message);
        response.setData(data);
        return response;
    }

    public static <T> ApiResponse<T> error(String message) {
        return error(ApiResultCode.BUSINESS_ERROR, message);
    }

    public static <T> ApiResponse<T> error(int code, String message) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    /**
     * 判断响应是否成功
     */
    public boolean isSuccess() {
        return this.code == ApiResultCode.SUCCESS;
    }
} 